﻿using System.Reflection;
using System.Reflection.Emit;

namespace ThingsGateway.SqlSugar;
/// <summary>
/// 动态构建器类
/// </summary>
public partial class DynamicBuilder
{
    /// <summary>
    /// 获取SplitTableAttribute的自定义属性构建器
    /// </summary>
    /// <param name="sugarTable">SplitTableAttribute实例</param>
    /// <returns>CustomAttributeBuilder实例</returns>
    internal CustomAttributeBuilder GetSplitEntityAttr(SplitTableAttribute sugarTable)
    {
        Type attributeType = typeof(SplitTableAttribute);
        ConstructorInfo attributeCtor = attributeType.GetConstructor(new Type[] { typeof(SplitType) });
        CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeCtor, new object[] { sugarTable.SplitType },
        new PropertyInfo[] {
attributeType.GetProperty(nameof(SplitTableAttribute.SplitType)),
        }
        , new object[] {
sugarTable.SplitType
        });
        return attributeBuilder;
    }

    /// <summary>  
    /// 获取SplitFieldAttribute的自定义属性构建器  
    /// </summary>  
    /// <param name="fieldAttribute">SplitFieldAttribute实例</param>  
    /// <returns>CustomAttributeBuilder实例</returns>  
    internal CustomAttributeBuilder GetSplitFieldAttr(SplitFieldAttribute fieldAttribute)
    {
        Type attributeType = typeof(SplitFieldAttribute);
        ConstructorInfo attributeCtor = attributeType.GetConstructor(Array.Empty<Type>());
        CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeCtor, Array.Empty<object>());
        return attributeBuilder;
    }

    /// <summary>  
    /// 获取SugarTable的自定义属性构建器  
    /// </summary>  
    /// <param name="sugarTable">SugarTable实例</param>  
    /// <returns>CustomAttributeBuilder实例</returns>  
    internal CustomAttributeBuilder GetEntity(SugarTable sugarTable)
    {
        Type attributeType = typeof(SugarTable);
        ConstructorInfo attributeCtor = attributeType.GetConstructor(new Type[] { typeof(string) });
        CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeCtor, new object[] { "" },
        new PropertyInfo[] {
          attributeType.GetProperty(nameof(SugarTable.TableName)),
          attributeType.GetProperty(nameof(SugarTable.TableDescription)) ,
          attributeType.GetProperty(nameof(SugarTable.IsDisabledUpdateAll)) ,
          attributeType.GetProperty(nameof(SugarTable.IsDisabledDelete)),
          attributeType.GetProperty(nameof(SugarTable.IsCreateTableFieldSort)),
          attributeType.GetProperty(nameof(SugarTable.Discrimator))
        }
        , new object[] {
            sugarTable.TableName,
            sugarTable.TableDescription ,
            sugarTable.IsDisabledUpdateAll,
            sugarTable.IsDisabledDelete,
            sugarTable.IsCreateTableFieldSort,
            sugarTable.Discrimator
         });
        return attributeBuilder;
    }

    /// <summary>  
    /// 获取SugarColumn的自定义属性构建器  
    /// </summary>  
    /// <param name="sugarTable">SugarColumn实例</param>  
    /// <returns>CustomAttributeBuilder实例</returns>  
    internal CustomAttributeBuilder GetProperty(SugarColumn sugarTable)
    {
        Type attributeType = typeof(SugarColumn);
        ConstructorInfo attributeCtor = attributeType.GetConstructor(Array.Empty<Type>());
        CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeCtor, Array.Empty<object>(),
        new PropertyInfo[] {
          attributeType.GetProperty(nameof(SugarColumn.IsPrimaryKey)),
          attributeType.GetProperty(nameof(SugarColumn.IsIdentity)),
          attributeType.GetProperty(nameof(SugarColumn.DefaultValue)),
          attributeType.GetProperty(nameof(SugarColumn.Length)),
          attributeType.GetProperty(nameof(SugarColumn.DecimalDigits)),
          attributeType.GetProperty(nameof(SugarColumn.ColumnDataType)),
          attributeType.GetProperty(nameof(SugarColumn.IsNullable)),
          attributeType.GetProperty(nameof(SugarColumn.ColumnDescription)),
          attributeType.GetProperty(nameof(SugarColumn.OracleSequenceName)),
          attributeType.GetProperty(nameof(SugarColumn.IsIgnore)),
          attributeType.GetProperty(nameof(SugarColumn.IsJson)),
          attributeType.GetProperty(nameof(SugarColumn.IsOnlyIgnoreInsert)),
          attributeType.GetProperty(nameof(SugarColumn.IsOnlyIgnoreUpdate)),
          attributeType.GetProperty(nameof(SugarColumn.OldColumnName)),
          attributeType.GetProperty(nameof(SugarColumn.SqlParameterDbType)),
          attributeType.GetProperty(nameof(SugarColumn.SqlParameterSize)),
          attributeType.GetProperty(nameof(SugarColumn.IsArray)),
          attributeType.GetProperty(nameof(SugarColumn.ColumnName)),
          attributeType.GetProperty(nameof(SugarColumn.InsertSql)),
          attributeType.GetProperty(nameof(SugarColumn.UpdateSql)),
          attributeType.GetProperty(nameof(SugarColumn.ExtendedAttribute)),
          attributeType.GetProperty(nameof(SugarColumn.IsDisabledAlterColumn)),
          attributeType.GetProperty(nameof(SugarColumn.IsOwnsOne)),
          attributeType.GetProperty(nameof(SugarColumn.InsertServerTime)),
          attributeType.GetProperty(nameof(SugarColumn.UpdateServerTime)),
          attributeType.GetProperty(nameof(SugarColumn.QuerySql))
        }
        , new object[] {
            sugarTable.IsPrimaryKey,
            sugarTable.IsIdentity,
            sugarTable.DefaultValue,
            sugarTable.Length,
            sugarTable.DecimalDigits,
            sugarTable.ColumnDataType,
            sugarTable.IsNullable,
            sugarTable.ColumnDescription,
            sugarTable.OracleSequenceName,
            sugarTable.IsIgnore,
            sugarTable.IsJson,
            sugarTable.IsOnlyIgnoreInsert,
            sugarTable.IsOnlyIgnoreUpdate,
            sugarTable.OldColumnName,
            sugarTable.SqlParameterDbType,
            sugarTable.SqlParameterSize,
            sugarTable.IsArray,
            sugarTable.ColumnName,
            sugarTable.InsertSql,
            sugarTable.UpdateSql,
            sugarTable.ExtendedAttribute,
            sugarTable.IsDisabledAlterColumn,
            sugarTable.IsOwnsOne,
             sugarTable.InsertServerTime,
              sugarTable.UpdateServerTime,
            sugarTable.QuerySql
         });
        return attributeBuilder;
    }
}